package Servlet;

import com.alibaba.fastjson.JSON;
import utils.BaseDao;
import utils.BaseModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddUserServlet",urlPatterns = {"/AddUser"})
public class AddUserServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object[] param = new Object[5];
        param[0] = req.getParameter("name");
        param[1] = req.getParameter("trueName");
        param[2] = req.getParameter("telephone");
        param[3] = req.getParameter("cardId");
        param[4] = req.getParameter("email");
        String sql = "insert into t_user (name,trueName,telephone,cardId,email) values(?,?,?,?,?)";
        int result = baseDao.update(sql,param);
        BaseModel baseModel = new BaseModel();
        if(result>0){
            baseModel.setCode(0);
            baseModel.setMassage("添加用户成功");
        }else {
            baseModel.setCode(-1);
            baseModel.setMassage("添加用户失败");
        }
        String baseModelStr = JSON.toJSONString(baseModel);
        resp.getWriter().write(baseModelStr);
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
